package com.primus.server.security.response;

import com.alibaba.fastjson.JSON;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录失败处理类
 */
@Component
public class SelfAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException {
        SecurityResponseBody responseBody = new SecurityResponseBody();

        responseBody.setStatus("300");
        responseBody.setMessage("Need Authorities!");

        httpServletResponse.setStatus(300);
        httpServletResponse.getWriter().write(JSON.toJSONString(responseBody));
    }
}
